#! /bin/sh

echo "Checking Assignment 3 (Part 2) Solutions" > part2-results.txt

# Validate your own version of the xml files.
  
echo "" >> part2-results.txt
echo "========== Checking xml files ==========" >> part2-results.txt

echo "" >> part2-results.txt
echo "------ bank.xml ------" >> part2-results.txt
echo "Well-formed? (no news is good news)" >> part2-results.txt
xmllint --noout bank.xml >> part2-results.txt  2>&1
echo "Valid? (no news is good news)" >> part2-results.txt
xmllint --noout --valid bank.xml >> part2-results.txt  2>&1
echo "Pretty version:" >> part2-results.txt
xmllint --format bank.xml >> part2-results.txt  2>&1

echo "" >> part2-results.txt
echo "------ quiz.xml ------" >> part2-results.txt
echo "Well-formed? (no news is good news)" >> part2-results.txt
xmllint --noout quiz.xml >> part2-results.txt  2>&1
echo "Valid? (no news is good news)" >> part2-results.txt
xmllint --noout --valid quiz.xml >> part2-results.txt  2>&1
echo "Pretty version:" >> part2-results.txt
xmllint --format quiz.xml >> part2-results.txt  2>&1

echo "" >> part2-results.txt
echo "------ class.xml ------" >> part2-results.txt
echo "Well-formed? (no news is good news)" >> part2-results.txt
xmllint --noout class.xml >> part2-results.txt  2>&1
echo "Valid? (no news is good news)" >> part2-results.txt
xmllint --noout --valid class.xml >> part2-results.txt  2>&1
echo "Pretty version:" >> part2-results.txt
xmllint --format class.xml >> part2-results.txt  2>&1

# Run your queries.

echo "" >> part2-results.txt
echo "========== Generating query output ==========" >> part2-results.txt

# First run the queries that we have no dtd for.
for query in q1 q2 q3 q4 q5 q6
do
   echo "" >> part2-results.txt
   echo "------ Query" $query "------" >> part2-results.txt
   echo "" >> part2-results.txt
   echo "Raw results:" >> part2-results.txt
   galax-run $query.xq >> part2-results.txt 2>&1
done

# Now run the queries that we can "pretty print" using xmllint
# (because we have a dtd).
for query in q7 q8
do
   echo "" >> part2-results.txt
   echo "------ Query" $query "------" >> part2-results.txt
   echo "" >> part2-results.txt
   echo "Raw results:" >> part2-results.txt
   galax-run $query.xq >> part2-results.txt  2>&1
   echo "<?xml version='1.0' standalone='no' ?>" > TEMP.xml
   galax-run $query.xq >> TEMP.xml  2>&1
   echo "" >> part2-results.txt
   echo "Pretty results:" >> part2-results.txt
   xmllint --format TEMP.xml >> part2-results.txt  2>&1
done

# Now validate the output of those queries.
echo "" >> part2-results.txt
echo "======= Validating xml generated by queries =======" >> part2-results.txt

echo "" >> part2-results.txt
echo "------ Query" q7 "------" >> part2-results.txt
echo "<?xml version='1.0' standalone='no' ?>" > TEMP.xml
echo "<!DOCTYPE Distribution SYSTEM 'distribution.dtd'>" >> TEMP.xml
galax-run q7.xq >> TEMP.xml  2>&1
echo "Results Well-formed? (no news is good news)" >> part2-results.txt
xmllint --noout TEMP.xml >> part2-results.txt  2>&1
echo "Results valid? (no news is good news)" >> part2-results.txt
xmllint --noout --valid TEMP.xml >> part2-results.txt  2>&1

echo "" >> part2-results.txt
echo "------ Query" q8 "------" >> part2-results.txt
echo "<?xml version='1.0' standalone='no' ?>" > TEMP.xml
echo "<!DOCTYPE ClassReport SYSTEM 'report.dtd'>" >> TEMP.xml
galax-run q8.xq >> TEMP.xml  2>&1
echo "Results Well-formed? (no news is good news)" >> part2-results.txt
xmllint --noout TEMP.xml >> part2-results.txt  2>&1
echo "Results valid? (no news is good news)" >> part2-results.txt
xmllint --noout --valid TEMP.xml >> part2-results.txt  2>&1
